//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package location;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import common.GlobalID;
import common.IDs;
import common.Logger;
import common.ObjectClass;
import init.StaticData;
import objects.ExpLevels;
import objects.ExpLevelsEntry;
import objects.ExtensionMaterialsEntry;
import objects.Silos;
import objects.SilosBarnMaterials;
import objects.Tools;
import objects.ToolsEntry;
import objects.WareHouse;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

abstract class LocationBase {
    protected LinkedHashMap<Integer, Integer> inventory;
    protected String locationObjects;
    protected Integer currentCoins;
    protected Integer currentCrystalls;
    protected Integer currentExp;
    protected Integer currentLevel;
    protected final String logName;

    protected JSONObject GOODS;
    protected JSONObject GOODSTEMP;

    public JSONObject getGOODS() {
        return GOODS;
    }

    protected abstract void reload(boolean var1);

    public LocationBase(LinkedHashMap<Integer, Integer> inventory, String locationObjects, String profileName) {
        this.logName = profileName;
        this.inventory = inventory;
        this.locationObjects = locationObjects;
    }

    private void resetCurrentCounters() {
        this.currentCoins = Integer.valueOf(0);
        this.currentCrystalls = Integer.valueOf(0);
        this.currentExp = Integer.valueOf(0);
        this.currentLevel = Integer.valueOf(0);
    }

    protected void updateCurrentResources() {
        this.resetCurrentCounters();
        Integer coins = (Integer)this.inventory.get(Integer.valueOf(1900000));
        Integer crystalls = (Integer)this.inventory.get(Integer.valueOf(1900001));
        Integer exp = (Integer)this.inventory.get(Integer.valueOf(1900003));
        Integer level = (Integer)this.inventory.get(Integer.valueOf(1900002));
        if(coins != null) {
            this.currentCoins = coins;
        }

        if(crystalls != null) {
            this.currentCrystalls = crystalls;
        }

        if(exp != null) {
            this.currentExp = exp;
        }

        if(level != null) {
            this.currentLevel = level;
        }

    }

    protected int getSilosCount(LinkedHashMap<Integer, Integer> inv) {
        int count = 0;
        if(inv != null) {
            Iterator var4 = inv.keySet().iterator();

            while(true) {
                Integer key;
                do {
                    if(!var4.hasNext()) {
                        return count;
                    }

                    key = (Integer)var4.next();
                } while(GlobalID.getClassID(key.intValue()) != 4 && GlobalID.getClassID(key.intValue()) != 61);

                count += ((Integer)inv.get(key)).intValue();
            }
        } else {
            return count;
        }
    }

    protected int getBarnCount(LinkedHashMap<Integer, Integer> inv) {
        int count = 0;
        if(inv != null) {
            Iterator var4 = inv.keySet().iterator();

            while(var4.hasNext()) {
                Integer key = (Integer)var4.next();
                if(ObjectClass.barnHash.get(Integer.valueOf(GlobalID.getClassID(key.intValue()))) != null) {
                    count += ((Integer)inv.get(key)).intValue();
                }
            }
        }

        return count;
    }

    protected int getSilosMax(LinkedHashMap<Integer, Integer> inv) {
        int nextSilos = this.getNextSilosLevel(inv);
        return Silos.getMaxForLevel(nextSilos - 1);
    }

    protected int getBarnMax(LinkedHashMap<Integer, Integer> inv) {
        int nextBarn = this.getNextBarnLevel(inv);
        return WareHouse.getMaxForLevel(nextBarn - 1);
    }

    protected int getSilosEmptyCount(LinkedHashMap<Integer, Integer> inv) {
        return this.getSilosMax(inv) - this.getSilosCount(inv);
    }

    protected int getBarnEmptyCount(LinkedHashMap<Integer, Integer> inv) {
        return this.getBarnMax(inv) - this.getBarnCount(inv);
    }

    protected int getNextSilosLevel(LinkedHashMap<Integer, Integer> inv) {
        Integer silosLevel = null;
        if(inv != null) {
            silosLevel = (Integer)inv.get(Integer.valueOf(1900006));
        }

        if(silosLevel == null) {
            silosLevel = Integer.valueOf(1);
        }

        return silosLevel.intValue();
    }

    protected int getNextBarnLevel(LinkedHashMap<Integer, Integer> inv) {
        Integer barnLevel = null;
        if(inv != null) {
            barnLevel = (Integer)inv.get(Integer.valueOf(1900007));
        }

        if(barnLevel == null) {
            barnLevel = Integer.valueOf(1);
        }

        return barnLevel.intValue();
    }

    public void updateCurrentExp(Integer expdelta) {
        ExpLevelsEntry e = ExpLevels.get(this.currentLevel.intValue());
        if(e != null) {
            Integer max = e.getExpToNextLevel();
            this.currentExp = Integer.valueOf(this.currentExp.intValue() + expdelta.intValue());
            if(this.currentExp.intValue() >= max.intValue()) {
                this.currentLevel = Integer.valueOf(this.currentLevel.intValue() + 1);
                this.currentExp = Integer.valueOf(this.currentExp.intValue() - max.intValue());
                e = ExpLevels.get(this.currentLevel.intValue());
                if(e != null) {
                    Iterator var5 = e.getRewards().keySet().iterator();

                    while(var5.hasNext()) {
                        String good = (String)var5.next();
                        Integer goodid = StaticData.getGlobalId(good);
                        if(goodid != null) {
                            if(IDs.isDiamonds(goodid)) {
                                goodid = Integer.valueOf(1900001);
                            }

                            if(IDs.isCoins(goodid)) {
                                goodid = Integer.valueOf(1900000);
                            }

                            Integer present = (Integer)this.inventory.get(goodid);
                            if(present == null) {
                                present = Integer.valueOf(0);
                            }

                            this.inventory.put(goodid, Integer.valueOf(present.intValue() + ((Integer)e.getRewards().get(good)).intValue()));
                        }
                    }

                    System.out.println(Logger.formatPT(this.logName, "  Level up rewards: " + e.getRewards()));
                    System.out.println(Logger.formatPT(this.logName, "  Level:" + this.currentLevel + ", Experience:" + this.currentExp + "/" + e.getExpToNextLevel() + ", Coins:" + this.currentCoins + ", Crystalls:" + this.currentCrystalls));
                }
            }
        }

    }

    private List<Integer> sortFromMaxToMin(final LinkedHashMap<Integer, Integer> inv) {
        ArrayList keyList = new ArrayList();
        Iterator var4 = inv.keySet().iterator();

        while(var4.hasNext()) {
            Integer key = (Integer)var4.next();
            keyList.add(key);
        }

        Collections.sort(keyList, new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return ((Integer)inv.get(o2)).intValue() - ((Integer)inv.get(o1)).intValue();
            }
        });
        return keyList;
    }

    protected void showBarnContent(LinkedHashMap<Integer, Integer> inv) {
        String bc = "  ";
        List keyList = this.sortFromMaxToMin(inv);
        int k = 1;
        Iterator var6 = keyList.iterator();

        JSONArray barnArr = new JSONArray();

        while(var6.hasNext()) {
            Integer key = (Integer)var6.next();
            if(ObjectClass.barnHash.get(Integer.valueOf(GlobalID.getClassID(key.intValue()))) != null) {
                Integer count = (Integer)inv.get(key);

                JSONObject item = new JSONObject();
                item.put("key", key);
                item.put("count", count);
                item.put("name", StaticData.getGlobalName(key));
                barnArr.add(item);

                bc = bc + "\"" + StaticData.getGlobalName(key) + "\":" + count + " ";
                if(k % 3 == 0) {
                    bc = bc + "\n  ";
                }

                ++k;
            }
        }

        --k;
        if(k % 3 != 0) {
            bc = bc + "\n";
        }

        GOODSTEMP.put("_BARN", barnArr);

        System.out.println(Logger.formatP(this.logName, bc));
    }

    protected void showSilosContent(LinkedHashMap<Integer, Integer> inv) {
        String bc = "  ";
        List keyList = this.sortFromMaxToMin(inv);
        int k = 1;
        Iterator var6 = keyList.iterator();

        JSONArray silosArr = new JSONArray();

        while(true) {
            Integer key;
            do {
                if(!var6.hasNext()) {
                    --k;
                    if(k % 3 != 0) {
                        bc = bc + "\n";
                    }

                    System.out.println(Logger.formatP(this.logName, bc));

                    GOODSTEMP.put("_SILOS", silosArr);
                    return;
                }

                key = (Integer)var6.next();
            } while(GlobalID.getClassID(key.intValue()) != 4 && GlobalID.getClassID(key.intValue()) != 61);

            Integer count = (Integer)inv.get(key);
            bc = bc + "\"" + StaticData.getGlobalName(key) + "\":" + count + " ";
            if(k % 3 == 0) {
                bc = bc + "\n  ";
            }

            ++k;

            JSONObject item = new JSONObject();
            item.put("key", key);
            item.put("count", count);
            item.put("name", StaticData.getGlobalName(key));
            silosArr.add(item);
        }
    }

    protected void showTable19Content(LinkedHashMap<Integer, Integer> inv) {
        String bc = "  ";
        List keyList = this.sortFromMaxToMin(inv);
        int k = 1;
        Iterator var6 = keyList.iterator();

        while(true) {
            Integer key;
            do {
                if(!var6.hasNext()) {
                    --k;
                    if(k % 3 != 0) {
                        bc = bc + "\n";
                    }

                    System.out.println(Logger.formatP(this.logName, bc));
                    return;
                }

                key = (Integer)var6.next();
            } while(key.intValue() != 1900009 && key.intValue() != 1900010 && key.intValue() != 1900011 && key.intValue() != 1900012);

            Integer count = (Integer)inv.get(key);
            bc = bc + "\"" + StaticData.getGlobalName(key) + "\":" + count + " ";
            if(k % 3 == 0) {
                bc = bc + "\n  ";
            }

            ++k;
        }
    }

    protected int showTools(LinkedHashMap<Integer, Integer> inv) {
        Integer toolsCount = Integer.valueOf(0);
        LinkedHashMap tools = new LinkedHashMap();
        Iterator var5 = Tools.getTools().values().iterator();

        JSONArray toolArr = new JSONArray();

        while(var5.hasNext()) {
            ToolsEntry te = (ToolsEntry)var5.next();
            Integer count = (Integer)inv.get(te.getGlobalID());
            if(count == null) {
                count = Integer.valueOf(0);
            }

            toolsCount = Integer.valueOf(toolsCount.intValue() + count.intValue());
            tools.put(te.getName(), count);

            JSONObject tool = new JSONObject();
            tool.put("key", te.getGlobalID().intValue());
            tool.put("name", te.getName());
            tool.put("count", count);

            toolArr.add(tool);
        }

        GOODSTEMP.put("_TOOLSTOTAL", toolsCount);
        GOODSTEMP.put("_TOOLS", toolArr);

        System.out.println(Logger.formatP(this.logName, "  Tools: " + toolsCount + " " + tools));
        return toolsCount.intValue();
    }

    protected int showMaterials(LinkedHashMap<Integer, Integer> inv) {
        Integer materialsCount = Integer.valueOf(0);
        LinkedHashMap materials = new LinkedHashMap();
        Iterator var5 = SilosBarnMaterials.getMaterials().iterator();

        JSONArray materialArr = new JSONArray();

        while(var5.hasNext()) {
            ExtensionMaterialsEntry me = (ExtensionMaterialsEntry)var5.next();
            Integer count = (Integer)inv.get(me.getGlobalID());
            if(count == null) {
                count = Integer.valueOf(0);
            }

            materialsCount = Integer.valueOf(materialsCount.intValue() + count.intValue());
            materials.put(me.getName(), count);

            JSONObject m = new JSONObject();
            m.put("key", me.getGlobalID().intValue());
            m.put("name", me.getName());
            m.put("count", count);

            materialArr.add(m);
        }

        GOODSTEMP.put("_MATERIALSTOTAL", materialsCount);
        GOODSTEMP.put("_MATERIALS", materialArr);

        System.out.println(Logger.formatP(this.logName, "  Materials: " + materialsCount + " " + materials));
        return materialsCount.intValue();
    }
}
